/**
 * 
 */
package com.movitech.mobile.cz.base.common.util;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.activiti.engine.impl.cfg.IdGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 封装各种生成唯一性ID算法的工具类.
 * @author ThinkGem
 * @version 2013-01-15
 */
@Service
@Lazy(false)
public class IdGenUtils implements IdGenerator {

    private static SecureRandom random = new SecureRandom();
    
    /**
     * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
     */
    public static String uuid() {
        return getUUID();//UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 当代-UUID
     * @return
     */
    public static String getUUID(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return "DPM"+simpleDateFormat.format(new Date()) + RandomStringUtils.random(5, "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }
    
    /**
     * 使用SecureRandom随机生成Long. 
     */
    public static long randomLong() {
        return Math.abs(random.nextLong());
    }

    /**
     * 使用SecureRandom随机生成Int. 
     */
    public static int randomInt() {
        return Math.abs(random.nextInt());
    }
    
    /**
     * 基于Base62编码的SecureRandom随机生成bytes.
     */
    public static String randomBase62(int length) {
        byte[] randomBytes = new byte[length];
        random.nextBytes(randomBytes);
        return Encodes.encodeBase62(randomBytes);
    }
    
    /**
     * Activiti ID 生成
     */
    
    public String getNextId() {
        return IdGenUtils.uuid();
    }
    
    public static void main(String[] args) {
        System.out.println(IdGenUtils.uuid());
        System.out.println(randomInt());
        System.out.println(new IdGenUtils().getNextId());
        for (int i=0; i<2; i++){
            System.out.println(IdGenUtils.randomLong() + "  " + IdGenUtils.randomBase62(5));
        }
    }

}
